package com.atguigu.ssyx.acl.service.impl;

import com.atguigu.ssyx.acl.mapper.RoleMapper;
import com.atguigu.ssyx.acl.service.AdminRoleService;
import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.model.acl.AdminRole;
import com.atguigu.ssyx.model.acl.Role;
import com.atguigu.ssyx.vo.acl.RoleQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Description:
 * Author Xu Chen xu
 * Create 2024/4/20 - 上午8:52
 * version v1.0
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private AdminRoleService adminRoleService;

    @Override
    public IPage<Role> selectPage(Page<Role> pageParam, RoleQueryVo roleQueryVo) {

        String roleName = roleQueryVo.getRoleName();

        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();

        if (!StringUtils.isEmpty(roleName)) {
            wrapper.like(Role::getRoleName, roleName);
        }
        return baseMapper.selectPage(pageParam, wrapper);
    }

    /**
     * 根据用户获取角色数据
     *
     * @param adminId
     * @return
     */
    @Override
    public Map<String, Object> findRoleByUserId(Long adminId) {
        List<Role> allRoleList = baseMapper.selectList(null);
        List<AdminRole> adminRoleList = adminRoleService.list(new QueryWrapper<AdminRole>()
                .eq("admin_id", adminId) // 根据 admin_id 查询
                .select("role_id"));//仅选择 role_id 字段
        List<Long> exitRoleIdList = adminRoleList.stream()// 将 adminRoleList 转换为 List<Long>
                .map(AdminRole::getRoleId)// 获取 role_id
                .collect(Collectors.toList());// 转换为 List
        List<Role> assignRoles = new ArrayList<>();

        // 遍历所有角色列表，判断当前用户拥有的角色，将这些角色添加到 assignRoles 中
        for (Role role : allRoleList) {
            if (exitRoleIdList.contains(role.getId())) {// 判断当前角色是否在 exitRoleIdList 中,如果在，则添加到 assignRoles 中
                assignRoles.add(role);
            }
        }

        // 返回结果
        Map<String, Object> map = new HashMap<>();
        map.put("allRolesList", allRoleList);
        map.put("assignRoles", assignRoles);
        return map;
    }


    @Override
    public void saveUserRoleRelationShip(Long adminId, Long[] roleId) {
        // 根据用户id删除角色数据
        adminRoleService.remove(new QueryWrapper<AdminRole>().eq("admin_id", adminId));

        // 遍历所有角色列表，判断当前用户拥有的角色，将这些角色添加到 assignRoles 中
        List<AdminRole> userRoleList = new ArrayList<>();
        for (Long role : roleId) {
            AdminRole adminRole = new AdminRole();
            adminRole.setAdminId(adminId);
            adminRole.setRoleId(role);
            userRoleList.add(adminRole);
        }
        // 批量添加
        adminRoleService.saveBatch(userRoleList);
    }
}
